﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using System.Net.Http;

namespace EasyUIDemo.MVC.Controllers
{
    public class DictionarysController : Controller
    {
        //
        // GET: /Dictionary/

        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 首页加载
        /// </summary>
        /// <returns></returns>
        public ActionResult Dictionary()
        {
            if (Session["dictionary"] == null)
            {
                return Content("<script>alert('请求出错');</script>");
            }
            return View((List<Dictionary>)Session["dictionary"]);
        }
        /// <summary>
        /// 添加页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult addDictionary(int? id)
        {
            if (id == null)
            {
                Dictionary<string, object> dropDic = Common.EnumListDic("请选择", "0");

                ViewBag.dropList = new SelectList(dropDic, "value", "key");

                return View();
            }
            else
            {
                Dictionary<string, object> dropDic = Common.EnumListDic("请选择", "0");
                var value = ((List<Dictionary>)Session["dictionary"]).Where(n => n.dictionary_Id == id).FirstOrDefault();
                ViewBag.dropList = new SelectList(dropDic, "value", "key", value.dictionary_Type);
                ViewBag.name = value.dictionary_Name;
                ViewBag.id = id;
                return View();
            }

        }
        /// <summary>
        /// 添加及修改
        /// </summary>
        /// <param name="droplist"></param>
        /// <param name="Dictionary_Name"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult addDictionary(string droplist, string Dictionary_Name, int? id)
        {
            if (id == null)
            {
                if (droplist == "0")
                {
                    return Content("<script>alert('请选择类型')<script>");
                }
                var count = ((List<Dictionary>)Session["dictionary"]).Where(n => n.dictionary_Type == droplist).OrderByDescending(n => n.dictionary_Id).FirstOrDefault();
                Dictionary dic = new Dictionary();
                dic.dictionary_Name = Dictionary_Name;
                dic.Dictionary_IdNumber = count.Dictionary_IdNumber + 1;
                dic.dictionary_Type = droplist;
                dic.dictionary_CreateTime = DateTime.Now;
                using (HttpClient client = new HttpClient())
                {
                    string str = "http://localhost:63805/api/Dictionary";
                    var context = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
                    var content = new StringContent(context);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    var result = client.PostAsync(str, content).Result;
                    if (result.IsSuccessStatusCode)
                    {
                        var it = result.Content.ReadAsStringAsync().Result;

                        if (it != "0")
                        {
                            return Content("<script>alert('添加成功');location.href='/Dictionarys/adddictionary'</script>");
                        }
                        else
                        {
                            return Content("<script>alert('添加失败');</script>");
                        }
                    }
                    return Content("<script>alert('请求出错');</script>");
                }
            }
            else
            {
                var count = ((List<Dictionary>)Session["dictionary"]).Where(n => n.dictionary_Id==id).FirstOrDefault();
                Dictionary dic = new Dictionary();
                dic.dictionary_Name = Dictionary_Name;
                dic.Dictionary_IdNumber = count.Dictionary_IdNumber;
                dic.dictionary_Type = droplist;
                dic.dictionary_CreateTime = DateTime.Now;
                //dic.dictionary_Id = Convert.ToInt32(id);
                using (HttpClient client = new HttpClient())
                {
                    string str = "http://localhost:63805/api/Dictionary/"+id;
                    var context = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
                    var content = new StringContent(context);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    var result = client.PutAsync(str, content).Result;
                    if (result.IsSuccessStatusCode)
                    {
                        var it = result.Content.ReadAsStringAsync().Result;

                        if (it != "0")
                        {
                            return Content("<script>alert('修改成功');location.href='/Dictionarys/adddictionary'</script>");
                        }
                        else
                        {
                            return Content("<script>alert('修改失败');</script>");
                        }
                    }
                    return Content("<script>alert('请求出错');</script>");
                }
            }


        }



    }
}
